package com.ettractions.kiosk.mediators
{
	import com.ettractions.kiosk.assets.Assets;
	import com.ettractions.kiosk.assets.AssetsStore;
	import com.ettractions.kiosk.components.BrochureMedia;
	import com.ettractions.kiosk.events.BrochureSelectEvent;
	import com.ettractions.kiosk.vo.BrochureVO;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	
	import mx.collections.ArrayList;
	
	import org.osmf.events.TimeEvent;
	import org.robotlegs.mvcs.Mediator;
		
	public class BrochureMediaMediator extends Mediator
	{
		private static const IMAGE_DISPLAY_DURATION:uint = 2000;
		
		// "Dots" List component is reporting incorrect width and not centering correctly; these hard-coded values allow for proper centering of that list
		private static const DOTS_LIST_ITEM_WIDTH:uint = 10;
		private static const DOTS_LIST_ITEM_GAP:uint = 4;
		
		[Inject]
		public var view:BrochureMedia;
		
		private var _imageStore:AssetsStore = new AssetsStore();

		private var _imageLoader:Loader = new Loader();
		
		private var _imageRotationTimer:Timer = new Timer(IMAGE_DISPLAY_DURATION, 1);
		
		private var _loadingImagePath:String;
		
		override public function onRegister():void
		{
			eventDispatcher.addEventListener(BrochureSelectEvent.BROCHURE_SELECTED, handleBrochureSelected);
			
			view.playVideoButton.addEventListener(MouseEvent.CLICK, handlePlayVideoButtonClick);
			
			_imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleImageLoaderComplete);
			
			_imageRotationTimer.addEventListener(TimerEvent.TIMER_COMPLETE, handleRotatorTimerComplete);			
		}
		
		private function handleBrochureSelected(event:BrochureSelectEvent):void
		{
			_imageStore.clear();
			
			_imageRotationTimer.reset();
			
			if (event.brochure != null)
			{
				var dotsDataProvider:ArrayList = new ArrayList();
				var firstContentImagePath:String = event.brochure.media.contentImagePaths[0];
				var firstContentImage:Bitmap = Assets.preloadedImages.get(firstContentImagePath);
				
				for each (var imagePath:String in event.brochure.media.contentImagePaths)
					dotsDataProvider.addItem(imagePath);
				
				view.data = event.brochure;
				
				view.image.source = firstContentImage;
					
				// Component is reporting incorrect width; using constant values to properly center it
				view.dots.x = Math.round((view.image.width / 2) - (((DOTS_LIST_ITEM_WIDTH * dotsDataProvider.length) + (DOTS_LIST_ITEM_GAP * (dotsDataProvider.length - 1))) / 2));

				view.dots.dataProvider = dotsDataProvider;
				view.dots.selectedIndex = 0;
				
				view.imageRotator.visible = true;
				
				view.video.visible = false;
				
				_imageStore.put(firstContentImagePath, firstContentImage);				
				
				_imageRotationTimer.start();
			}
		}
		
		private function handlePlayVideoButtonClick(event:MouseEvent):void
		{
			_imageRotationTimer.reset();
			
			view.imageRotator.visible = false;
			
			view.video.visible = true;
			view.video.play();
		}
		
		private function handleVideoComplete(event:TimeEvent):void
		{
			view.video.visible = false;
			
			view.imageRotator.visible = true;
			
			view.dots.selectedIndex = 0;
			
			_imageRotationTimer.start();
		}
		
		private function handleImageLoaderComplete(event:Event):void
		{
			var bitmap:Bitmap = Bitmap(LoaderInfo(event.target).content);
			var index:uint = view.dots.selectedIndex + 1;
			
			view.image.source = bitmap;
			
			view.dots.selectedIndex = index;
			
			_imageStore.put(_loadingImagePath, bitmap);
			
			_imageRotationTimer.start();			
		}
		
		private function handleRotatorTimerComplete(event:TimerEvent):void
		{
			var dp:ArrayList = ArrayList(view.dots.dataProvider);
			var index:uint = view.dots.selectedIndex + 1;
			var bitmap:Bitmap;
			
			if (index == dp.length)
				index = 0;
			
			_imageRotationTimer.reset();			
			
			_loadingImagePath = String(dp.getItemAt(index));
			
			bitmap = _imageStore.get(_loadingImagePath);
			
			if (bitmap != null)
			{
				view.image.source = bitmap;
				
				view.dots.selectedIndex = index;				
				
				_imageRotationTimer.start();
			}
				
			else
				_imageLoader.load(new URLRequest(_loadingImagePath));
		}
	}
}